package br.com.hermes.bt;


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import br.com.hermes.emu.bt.EmuBlue;

public class EmuBlueTestUI extends JFrame implements ActionListener, HmListener {

	private static final long serialVersionUID = 7096990910144755805L;
	private JButton btCreate;
	private JButton btJoin;
	private JButton btConnect;
	private JButton btSend;

	private JTextField text;
	private JTextArea textArea;
	
	private JDesktopPane desktop;
	private JLabel status;
	private JComboBox jdevices;
	private HermesBlue hermesBlue;
	private Vector<HmBluetoothDevice> devicesDiscovered;
	
	public EmuBlueTestUI() {
		setTitle("HermesEmuBlueTest 1.0.0");

		setLayout(null);
		btCreate = new JButton("Create");
		btCreate.setActionCommand("create");
		btCreate.setBounds(40, 320, 140, 50);
		btCreate.addActionListener(this);

		btJoin = new JButton("Join");
		btJoin.setActionCommand("join");
		btJoin.setBounds(210, 320, 140, 50);
		btJoin.addActionListener(this);
		
		status = new JLabel("Status:");
		status.setBounds(40, 8, 310, 50);
		status.setForeground(Color.RED);

		 
		desktop = new JDesktopPane();
		desktop.setBounds(40, 40, 310, 270);
		desktop.setBackground(new Color(0, 195, 255));
		
		text = new JTextField();
		text.setBounds(10, 220, 200, 40);
		
		btSend = new JButton("Send");
		btSend.setActionCommand("send");
		btSend.setBounds(220, 220, 80, 40);
		btSend.addActionListener(this);
		
		textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setAutoscrolls(true);
		
		JScrollPane scroll = new JScrollPane(textArea);  
		scroll.setBounds(10, 60, 290, 140);
		
		desktop.add(text);
		desktop.add(btSend);
		desktop.add(scroll);
		
		add(btCreate);
		add(btJoin);
		add(status);
		add(desktop);

		setSize(400, 400);
		
		hermesBlue   =  new EmuBlue();
		hermesBlue.addHermesListener(this);
		devicesDiscovered = new Vector<HmBluetoothDevice>();
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getActionCommand().equals("create")) {
		  hermesBlue.initService();
		  hermesBlue.startService();
		}
		else if (e.getActionCommand().equals("join")) {
			hermesBlue.startDiscovery();
		}
		else if(e.getActionCommand().equals("conectar")){
			int selected= jdevices.getSelectedIndex();
			if (selected >= 0){
				
				HmBluetoothDevice bluetoothDevice = searchBluetoothDeviceByName((String)jdevices.getItemAt(selected));
				if(bluetoothDevice!=null){
					try {
						hermesBlue.connect(bluetoothDevice);
					} catch (HmException e1) {
						e1.printStackTrace();
					}
				}
			}
		}else if(e.getActionCommand().equals("send")){
			
			try {
				hermesBlue.write(text.getText().getBytes());
				textArea.setText(textArea.getText() + " OUT: " + text.getText() + "\n");
				text.setText("");
			} catch (HmException e1) {
				JOptionPane.showMessageDialog(null, e1.getMessage());
			}
		}
	}

	@Override
	public void notifyDevicesDiscovered(
			Vector<HmBluetoothDevice> devicesDiscovered) {
		showStatus("Dispositivos encontrados");
		this.devicesDiscovered = devicesDiscovered;
		showDevices();
	}

	@Override
	public void notifyMessage(E_MessageType type, String message){
		showStatus(message);
  }
	
	@Override
	public void notifyData(byte data[]){
		textArea.setText(textArea.getText() + " IN: " + new String(data) + "\n");
	}

	
	private void showDevices(){
		Vector<String> listNames = new Vector<String>();
		for (HmBluetoothDevice dev: devicesDiscovered)
			listNames.add(dev.getName());
		
		jdevices = new JComboBox(listNames);
		jdevices.setBounds(10, 10, 180, 40);

		btConnect= new JButton("Conectar");
		btConnect.setActionCommand("conectar");
		btConnect.setBounds(195, 10, 95, 40);
		btConnect.addActionListener(this);
		
		desktop.add(jdevices);
		desktop.add(btConnect);
	}
  
	private void showStatus(String message){
		status.setText("Status: " +message);
	}
	
	private HmBluetoothDevice searchBluetoothDeviceByName(String name){
		HmBluetoothDevice bluetoothDevice = null;
		for (HmBluetoothDevice dev: devicesDiscovered){
			if(dev.getName().equals(name)){
				bluetoothDevice = dev;
				break;
			}
		}
		
		return bluetoothDevice;
	}

	@Override
	public void notifyIsRead() {
		
	}

}
